package com.edward.service;


import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.edward.PagerModel;
import com.edward.model.DrawBack;
import com.edward.model.UnDb;



public class DrawBackManager extends HibernateDaoSupport {
	
	@SuppressWarnings("unchecked")
	public void add(DrawBack drawBack) {
		
		List<UnDb> l = getHibernateTemplate().find("from UnDb u where u.code = '" + drawBack.getCode() + "'" );
		l.get(0).setTag(1);
		getHibernateTemplate().update(l.get(0));
		getHibernateTemplate().save(drawBack);
	}
	
	public void update(DrawBack drawBack){
		getHibernateTemplate().update(drawBack);
	}
	
	public DrawBack getDrawBack(int id){
		DrawBack d =(DrawBack)getHibernateTemplate().get(DrawBack.class, id);
		return d;
	}
	
	
	@SuppressWarnings("unchecked")
	public double getSum(){
		List <DrawBack>list = getHibernateTemplate().find("from DrawBack");
		double sum = 0;
		for(DrawBack db : list){
			sum += db.getSum();
		}
		return sum;
	}
	
	
	@SuppressWarnings("unchecked")
	public List<DrawBack> listDrawBack() {
		return getHibernateTemplate().find("from DrawBack");
	}

	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit) {
		List <DrawBack>list = getHibernateTemplate().find("from DrawBack" );
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <DrawBack>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}



}



